package net.thxjj.adminServer.util;

import com.alibaba.fastjson.JSONArray;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;

public class AwsSigture {
    private static final String ACCESS_KEY = "AKIAZJ5FZV27XOROK566";
    private static final String SECRET_KEY = "AKIAZJ5FZV27XOROK566";
    private static final String region = "us-west-1";
    private static final String serviceName = "iotdata";
    public  static final  String host = "mqtt.stage.kidde-remotelync.com";

    public String dateString;
    public String Authorization;

    static byte[] HmacSHA256(String data, byte[] key) throws Exception {
        String algorithm="HmacSHA256";
        Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec
                (key, algorithm));
        return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
    }

    static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
        byte[] kSecret = ("AWS4" + key).getBytes(StandardCharsets.UTF_8);
        byte[] kDate = HmacSHA256(dateStamp, kSecret);
        byte[] kRegion = HmacSHA256(regionName, kDate);
        byte[] kService = HmacSHA256(serviceName, kRegion);
        byte[] kSigning = HmacSHA256("aws4_request", kService);
        return kSigning;
    }

    static String getEventContent(byte[] datas){

        StringBuilder ur = new StringBuilder();
        for(int i=0;i<datas.length;i++){
            long b = datas[i];
            if(b<0) b = 256+b;
            String a = String.format("%02x",b);
            ur.append(a.toLowerCase());
        }

        return ur.toString();
    }

    public static void main(String[] args){
        try {
            AwsSigture ad =new AwsSigture();
            ad.cal();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        try {
//            String signature = getEventContent(getSignatureKey(SECRET_KEY,"20211019T084349Z",region,serviceName));
//            System.out.println("signature:"+signature);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public void cal() throws Exception{
        Calendar da = Calendar.getInstance();
        da.add(Calendar.HOUR,-8);
        long timechuo = da.getTime().getTime();

        String dateTime = DateUtil.formatTimeStamp(timechuo);
        dateTime = dateTime.replace(" ","T");
        dateTime = dateTime+"Z";
        this.dateString = dateTime;
        System.out.println("dateString:"+this.dateString);
        String dateTime2 = DateUtil.formatTimeStamp2(timechuo);

        String signature = getEventContent(getSignatureKey(SECRET_KEY,dateTime2,region,serviceName));

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("AWS4-HMAC-SHA256 Credential=");
        stringBuilder.append(ACCESS_KEY);
        stringBuilder.append("/");
        stringBuilder.append(dateTime2);
        stringBuilder.append("/");
        stringBuilder.append(region);
        stringBuilder.append("/");
        stringBuilder.append(serviceName);
        stringBuilder.append("/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=");
        stringBuilder.append(signature);

        this.Authorization = stringBuilder.toString();
        System.out.println("Authorization:"+this.Authorization);
    }
}
